﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LP580.CG;

public partial class Manage_CG_GiftProductClassOperate : System.Web.UI.Page
{
    private string act = Utils.RQ_str("act").ToLower();
    private int id = Utils.RQ_int("id");
    int ParentId = Utils.RQ_int("pid");
    protected void Page_Load(object sender, EventArgs e)
    {
        clsAdmin adm = clsAdmin.getCookieAdmin();
        if (adm == null)
        {
            Utils.WrAlertGo("您尚未成功登陆，请成功登陆后再进行正常操作！", "../Login.aspx");
        }

        if (!this.IsPostBack)
            bindData();
        bindContrl();
    }

    private void bindContrl()
    {

    }

    private void bindData()
    {
        if (Request.UrlReferrer != null)
            hfdFromUrl.Value = Request.UrlReferrer.ToString();

        ddlParent.Items.Add(new ListItem("顶级类别", "0"));
        ddlParent.AppendDataBoundItems = true;
        ddlParent.DataSource = clsGiftProductClass.GetCollection();
        ddlParent.DataValueField = "GiftProductClassID";
        ddlParent.DataTextField = "TreeName";
        ddlParent.DataBind();
        ddlParent.Attributes.Add("size","20");
        if (act == "edit")
        {
            ltMgTitle.Text = "修改产品分类";
            ltPathTitle.Text = "[修改产品分类]";
            ltOperTitle.Text = "修改产品分类";
            clsGiftProductClass cls = new clsGiftProductClass(id);
            tbxGiftProductClassName.Text = cls.GiftProductClassName;
            tbxGiftProductClassCode.Text = cls.GiftProductClassCode;
            clsGiftProductClass parent=cls.Parent;
            if (parent != null)
            {
                ListItem li = ddlParent.Items.FindByValue(cls.Parent.GiftProductClassID.ToString());
                if (li != null) li.Selected = true;
            }
            btnSave.Text = " 修 改 ";
        }
        else
        {
            ltMgTitle.Text = "添加产品分类";
            ltPathTitle.Text = "[添加产品分类]";
            ltOperTitle.Text = "添加产品分类";
            ListItem li = ddlParent.Items.FindByValue(ParentId.ToString());
            if (li != null) li.Selected = true;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string name = tbxGiftProductClassName.Text.Trim();
        if (name == "")
        {
            Utils.WrAlertGoBack("标注*号为必填不可为空！");
        }
        clsGiftProductClass cls = new clsGiftProductClass(id);
        cls.GiftProductClassName = name;

        int parentid = 0;
        if (!int.TryParse(ddlParent.SelectedValue, out parentid))
            parentid = 0;

        if (act.ToLower() == "edit")
        {
            clsGiftProductClass.Update(cls, parentid);
        }
        else
        {
            clsGiftProductClass.Insert(cls, parentid);
        }
        string goUrl = hfdFromUrl.Value;
        if (goUrl == "")
        {
            goUrl = "GiftProductClassList.aspx";
        }
        Response.Redirect(goUrl, true);
    }
}